<template>
  <a-card>
    <!-- 高级查询组件 -->
    <m-e-s-super-query
      :fieldList='queryObj'
      :saveCode='fileName'
      @ok='handleSuperQuery'
    ></m-e-s-super-query>
    <v-table
      :toolbar='true'
      bordered
      ref='table'
      :data='loadData'
      :tableColumn='tableColumns'
    >
      <template v-slot:buttons>
        <a-button @click='addData' icon='plus' type='primary'>新建</a-button>
        <a-button @click='showModal' icon='download' type='plain' style='margin-left: 12px'>导入向导</a-button>
        <a-button @click='exportDataList' icon='download' type='plain' style='margin-left: 12px'>导出</a-button>
      </template>
      <template v-slot:action='{ text, row }'>
        <span>
          <a href='javascript:' @click='showData(row)'>查看</a>
        </span>
        <span>
          <a-divider type='vertical' />
          <a href='javascript:' @click='editData(row)'>编辑</a>
        </span>
        <span>
          <a-divider type='vertical' />
          <a-popconfirm title='是否删除' @confirm='deleteItem(row)' okText='是' cancelText='否'>
            <a-icon slot='icon' type='question-circle-o' style='color: red' />
            <a href='javascript:' style='color: red'>删除</a>
          </a-popconfirm>
        </span>
      </template>
    </v-table>
    <!-- form表单 -->
    <modal-form
      @ok='queryPage'
      ref='modalForm'
    />
    <!-- 导入 -->
    <b-import-file
      @ok='queryPage'
      :actionUrl='actionUrl'
      :des='des' ref='BImportFile'
      @download='download' />
  </a-card>
</template>

<script>
import { TableMixin } from '@/mixins/TableMixin'
import { queryObj,tableColumns } from './positionBaseTemplate'
import { superPageFetchList,delObj,exportData,downloadExcelCandidate } from '@api/base/positionBase'
import MESSuperQuery from '@comp/MesWSuperQuery/MesWSuperQuery'
import modalForm from './PositionAddOrUpdate'
import BImportFile from '@comp/BImportFile/BImportFile'
export default {
  name: 'positionList',
  mixins: [TableMixin],
  components: {
    MESSuperQuery,
    BImportFile,
    modalForm
  },
  data() {
    return {
      queryObj,
      tableColumns,
      fileName: 'warehouseList',
      warehouseTypes: [],
      // 导入接口
      actionUrl: '/baseWarehousePosition/importExcel',
      des: ' <h3>说明</h3>\n' +
        '      <h4>请下载导入模板，填写完成后导入</h4>',
      loadData: parameter => {
        return superPageFetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })

      }
    }
  },
  methods: {
    handleSuperQuery(params) {
      this.queryParam.queryType = params.queryType
      this.queryParam.queryItems = encodeURI(JSON.stringify(params.queryItems))
      this.queryPage()
    },
    // 删除
    deleteItem(record) {
      delObj(record.id).then(() => {
        this.$message.info('删除成功')
        this.queryPage()
      })
    },
    //下载导入模板
    download() {
      downloadExcelCandidate().then(res => {
        const blbo = new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' })
        const link = document.createElement('a')
        link.href = window.URL.createObjectURL(blbo)
        link.download =  '库位导入模板.xls'
        link.click()
      }).catch((e) => {
        this.$message.warning('下载失败')
      })
    },
    //导出表格
    exportDataList() {
      exportData(this.queryParam).then(res => {
        //创建二进制数据对象
        const blbo = new Blob([res], { type: 'application/vnd.ms-excel;charset=utf-8' })
        //创建a标签
        const link = document.createElement('a')
        //创建一个指向二进制文件的URL
        link.href = window.URL.createObjectURL(blbo)
        link.download = '库位导出信息.xls'
        link.click()
      })
    }
  }
}
</script>

<style scoped>

</style>